package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.AutoCompleteTextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.google.gson.Gson;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.FindCarDeliveryInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.lite.FindCarLiteDeliverActivity;
import com.tianhui.consignortwofei.mvp.ui.adapter.FindCarListAdapter;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/8/10
 * @description
 */
public class FindCarDeliveryListActivity extends BaseDataListActivity {

    @BindView(R.id.layout_search_editText)
    AutoCompleteTextView searchEditText;

    private DriverModel driverModel;

    private String mSearchKey;


    @Override
    public int getLayoutId() {
        return R.layout.activity_findcar_delivery_list;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new FindCarListAdapter(getContext());
    }

//    private void setResult(DriverInfo driverInfo) {
//        Intent intent = new Intent();
//        intent.putExtra(KeyValue.KEY_NAME, driverInfo.drivername);
//        intent.putExtra(KeyValue.KEY_PHONE, driverInfo.drivertel);
//        intent.putExtra(KeyValue.KEY_VEHICLE_NUMBER, driverInfo.defaultvehiclenum);
//        setResult(KeyValue.KEY_RESULT_CODE, intent);
//        finish();
//    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                DriverInfo driverInfo = (DriverInfo) mAdapter.getAllData().get(position);
//                if (driverInfo != null && !TextUtils.isEmpty(driverInfo.drivertel)) {
//                    setResult(driverInfo);
//                }
                FindCarDeliveryInfo.CarRecords carRecords = allList.get(position);
                Gson gson=new Gson();
                String json = gson.toJson(carRecords);

                Bundle bundle=new Bundle();
                bundle.putString("erpdetails",json);
                openActivity(FindCarLiteDeliverActivity.class,bundle);



            }
        });
    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        mSearchKey = searchEditText.getText().toString();
//        mSearchKey="202212";

        if (checkSearchKey(mSearchKey)) {
            getSearchCarList(mSearchKey, isDialog,page);
        } else {
            stopRefresh();
        }
    }

    @Override
    public void initData() {
        setTitle("找车单列表");

        searchEditText.setHint("请输入单号");
        setPageInfo(1, 1);
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.layout_search_textView)
    public void searchDriverDetail() {

        mSearchKey = searchEditText.getText().toString();
        if (checkSearchKey(mSearchKey)) {
            setRefresh(true);
            getSearchCarList(mSearchKey, true,1);
        }
    }

    private boolean checkSearchKey(String searchKey) {
        if (TextUtils.isEmpty(searchKey)) {
            return false;
//            showToast("请输入正确的单号");
        } else  {
            return true;
        }
//        return false;


//        if (TextUtils.isEmpty(searchKey)) {
//            showEmptyView();
//        } else if (CheckUtil.isMobileNO(mSearchKey)) {
//            return true;
//        } else {
//            showToast("请输入正确的单号");
//        }

    }


    private List<FindCarDeliveryInfo.CarRecords> allList=new ArrayList<>();
    public void getSearchCarList(String code, boolean isDialog,int page) {
        if (null==allList){
            allList=new ArrayList<>();
        }

        String consignorName = SharedPreferenceManager.getInstance().getConsignorName();
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        map.put("page", page + "");
        map.put("limit", "15");
        map.put("offset ", "0");

        map.put("personname", consignorName);

        map.put("sort", "voucherdate");
        map.put("order", "desc");


        //http://124.239.149.36:10611/tianhui/erpware/viewSASaleDelivery
        if (driverModel == null) {
            driverModel = new DriverModel();
        }

        driverModel.getSearchCarList(this, map, isDialog, bindLifecycle(), new ObserverResponseListener<CommonResponse<FindCarDeliveryInfo>>() {
            @Override
            public void onNext(CommonResponse<FindCarDeliveryInfo> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        allList.clear();
                        List<FindCarDeliveryInfo.CarRecords> list = commonResponse.getData().getRecords();
//                        List<DriverInfo> list = commonResponse.getData();
//                        List<DriverInfo> resultList = new ArrayList<>();

                        if (list != null && list.size() > 0) {
                            allList.addAll(list);

//                            setPageInfo(1, 1);
//                            for (DriverInfo info : list) {
//                                if (info.drivertel.equals(code)) {
//                                    resultList.add(info);
//                                }
//                            }
                            if (list.size() > 0) {

                                showDataList(list);
                            } else {
                                showEmptyView();
                            }

                        } else {
                            showEmptyView();
                        }

                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }
            }


            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });


//        driverModel.assignDriverListModel(this, map, isDialog, bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DriverInfo>>>() {
//            @Override
//            public void onNext(CommonResponse<List<DriverInfo>> commonResponse) {
//                if (!isFinishing() && !isDestroyed()) {
//                    if (commonResponse.getCode() == 20000) {
//                        List<DriverInfo> list = commonResponse.getData();
//                        List<DriverInfo> resultList = new ArrayList<>();
//
//                        if (list != null && list.size() > 0) {
//                            setPageInfo(1, 1);
//                            for (DriverInfo info : list) {
//                                if (info.drivertel.equals(phone)) {
//                                    resultList.add(info);
//                                }
//                            }
//                            if (list.size() > 0) {
//                                showDataList(list);
//                            } else {
//                                showEmptyView();
//                            }
//
//                        } else {
//                            showEmptyView();
//                        }
//
//                    } else {
//                        showToast(commonResponse.getMsg());
//                    }
//                }
//            }
//
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });
    }
}